
package code.codeworld.common.res.service.impl;

import cn.hutool.core.date.DateUtil;
import code.codeworld.common.res.mapper.ResJPA;
import code.codeworld.common.res.mapper.ResMapper;
import code.codeworld.common.res.model.Res;
import code.codeworld.common.res.model.vo.ResVo;
import code.codeworld.common.res.service.ResService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * ResService接口实现类
 *
 * @author freecode
 * @date 2023/03/23 00:20
 **/
@Service
public class ResServiceImpl extends ServiceImpl<ResMapper, Res> implements ResService {
    @Resource
    private ResJPA ResJPA;

    @Override
    public Page<Res> page(ResVo resVo) {
        Page<Res> page = ResJPA.findAll(resVo.getPageable());
        page.getContent().forEach(t -> {
            if (null == t.getBlurb() || "".equals(t.getBlurb())) {
                if (t.getContent().length() > BigDecimal.ROUND_CEILING * BigDecimal.TEN.intValue()) {
                    t.setBlurb(t.getContent().substring(BigDecimal.ROUND_UP, BigDecimal.ROUND_CEILING * BigDecimal.TEN.intValue()) + "..");
                } else {
                    t.setBlurb(t.getContent());
                }
            }
        });
        return page;
    }

    @Override
    public void add(Res res) {
        ResJPA.save(res);
    }

    @Override
    public void edit(Res res) {
        Res theRes = this.queryEntity(res.getId());
        theRes.setNotNull(res);
        ResJPA.save(theRes);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<Res> resList) {

        // 执行删除
    }

    @Override
    public Res detail(Res res) {
        return this.queryEntity(res.getId());
    }

    @Override
    public Res queryEntity(Long id) {
        return ResJPA.getReferenceById(id.intValue());
    }

}
